package com.masterstroke.template.controller;

import com.masterstroke.template.common.constant.MessageConstant;
import com.masterstroke.template.common.exception.BaseException;
import com.masterstroke.template.common.result.Result;
import com.masterstroke.template.pojo.domain.Product;
import com.masterstroke.template.pojo.dto.BuyProduct;
import com.masterstroke.template.pojo.dto.InsertProductDTO;
import com.masterstroke.template.pojo.vo.ShowOrder;
import com.masterstroke.template.service.OrderService;
import com.masterstroke.template.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController("product")
@RequestMapping("product")
@Api("电脑配件相关接口")
@Slf4j
public class PartController {

    @Autowired
    private ProductService productService;

    @Autowired
    private OrderService orderService;

    @ApiOperation("添加电脑配件")
    @PostMapping("addProduct")
    public Result addProduct(@RequestBody InsertProductDTO insertProduct) {
        log.info("添加信息为：{}", insertProduct);
        productService.insert(insertProduct);
        return Result.success();
    }

    @ApiOperation("根据id查询配件")
    @GetMapping("getProduct")
    public Result<Product> selectProduct(String productId) {
        Product byId = productService.getById(productId);
        log.info("查询到的信息：{}",byId);
        return Result.success(byId);
    }

    @ApiOperation("更新配件信息")
    @PostMapping("updateProduct")
    public Result updateProduct(@RequestBody Product product) {
        Result<Product> productResult = selectProduct(product.getProductId());
        if(productResult.getData()==null)
            throw new BaseException(MessageConstant.UNKNOWN_ERROR);
        productService.updateById(product);
        return Result.success();
    }

    @ApiOperation("删除配件信息")
    @DeleteMapping("deleteProduct")
    public Result deleteProduct(String productId) {
        productService.removeById(productId);
        return Result.success();
    }

    @ApiOperation("查询全部配件信息")
    @GetMapping("getAll")
    public Result<List<Product>> getAll() {
        List<Product> list = productService.list();
        return Result.success(list);
    }
    @ApiOperation("购买电脑配件")
    @PostMapping("buyProduct")
    public Result buyProduct(@RequestBody BuyProduct buyProduct) {
        productService.buyProduct(buyProduct);
        return Result.success();

    }

    @ApiOperation("获取订单")
    @GetMapping("getOrder")
    public Result<List<ShowOrder>> addOrder(){
        log.info("获取订单");
        return Result.success(orderService.getAllOrder());
    }
}

